package com.site.action.backend.system;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import com.site.dto.backend.system.ResourseBizBean;
import com.site.service.backend.system.MenuService;
import com.site.util.base.action.BackendBaseAction;
import com.site.util.base.service.BaseService;
import com.site.util.exception.ApplicationException;

/**
 * 菜单action
 * @author Lee 2012-12-23
 *
 */
public class MenuAction extends BackendBaseAction {
 
	private static final long serialVersionUID = -4832306786488118708L;

	private MenuService menuService;
	
	private ResourseBizBean resourseBizBean;
	
	/**
	 * 
	 * @return
	 */
	public String getMenuPage(){
		return "getMenuPage";
	}
	/**
	 * @return
	 * @throws IOException 
	 */
	public String getMenuJson() throws IOException {
		List<ResourseBizBean>  resourseBizBeanList = menuService.getMenuList(queryPage, resourseBizBean);
		// { id:0, pId:-1, name:"选择菜单", open:false},
		String html = "[";
		if(null != resourseBizBeanList && resourseBizBeanList.size() > 0){
			for (ResourseBizBean vo : resourseBizBeanList) {
				if(null != vo){
					//if(null != vo.getChildrenList() && vo.getChildrenList().size() > 0 ){
						//html += "{\"id\":\""+vo.getId()+"\",\"pId\":\"0\",\"name\":\""+vo.getTitle()+"\",\"URL\":\""+vo.getUrl()+"\",\"iconCls\":\"task\",\"leaf\":\"false\",\"expanded\": \"true\",\"children\":[";
						//html += getClidren(vo)+"]},";
					//} else {
						if(null != resourseBizBean && resourseBizBean.getName().equals("root")){
							html += "{\"id\":\""+vo.getId()+"\",\"pId\":\"0\",\"name\":\""+vo.getTitle()+"\",\"URL\":\""+vo.getUrl()+"\",\"orderBy\":\""+vo.getOrderBy()+"\",\"iconCls\":\"task-folder\",\"leaf\":\"false\",\"expanded\": \"false\"},";
						} else {
							html += "{\"id\":\""+vo.getId()+"\",\"pId\":\"0\",\"name\":\""+vo.getTitle()+"\",\"URL\":\""+vo.getUrl()+"\",\"orderBy\":\""+vo.getOrderBy()+"\",\"iconCls\":\"task\",\"leaf\":\"true\",\"expanded\": \"false\"},";
						}
					//}
				}
			}
			html= html.substring(0, html.length()-1);
		} 
		System.out.println(html+"]");
		response.getWriter().print(html+"]");
		return null;
	}
	
	/**
	 * 递归获得子节点的HTML
	 * (这里暂时没有用到)
	 * @param vo
	 * @return
	 */
	@SuppressWarnings("unused")
	private String getClidren(ResourseBizBean vo){
		String html = "";
		for (ResourseBizBean vo1 : vo.getChildrenList()) {
			if(null != vo1){
				if(null != vo1.getChildrenList() && vo1.getChildrenList().size() > 0){
					html += "{\"id\":\""+vo1.getId()+"\",\"pId\":\""+vo1.getParentResourseBizBean().getId()+"\",\"name\":\""+vo1.getTitle()+"\",\"URL\":\""+vo.getUrl()+"\",\"iconCls\":\"task\",\"leaf\":\"false\",\"children\":[";
					html += getClidren(vo1)+"]},";
				} else {
					html += "{\"id\":\""+vo1.getId()+"\",\"pId\":\""+vo1.getParentResourseBizBean().getId()+"\",\"name\":\""+vo1.getTitle()+"\",\"URL\":\""+vo.getUrl()+"\",\"leaf\":\"true\"},";
				}
			}
		}
		return html.substring(0,html.length()-1);
	}
	/**
	 * 更新菜单
	 * @author Lee 2012-12-25
	 * @return
	 */
	public String update() throws IOException{
		try {
			if(null != resourseBizBean && resourseBizBean.getId() !=null){
				menuService.execute(resourseBizBean, BaseService.COMMAND_UPDATE);
				response.getWriter().print("{\"msg\":true}");
				log.info("菜单更新成功……");
			} else {
				response.getWriter().print("{\"msg\":false}");
				log.info("菜单id不能为空……");
			}
		} catch (ApplicationException e) {
			throw (e);
		}
		
		return null;
	}
	/**
	 * 删除菜单（逻辑删除）
	 * @author Lee 2012-12-25
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception{
		try {
			if(null != resourseBizBean && null != resourseBizBean.getId()){
				menuService.execute(resourseBizBean, BaseService.COMMAND_DELETE);			
				response.getWriter().print("{\"result\":true}");
				log.info("逻辑删除菜单成功。。。");
			} else {
				response.getWriter().print("{\"result\":false}");
				log.info("菜单实体为空,逻辑删除菜单失败！");
			}
		} catch (Exception e) {
			 e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 增加菜单
	 * @author Lee 2013-01-08
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception{
		try {
			if(null != resourseBizBean){
				resourseBizBean.setCreatedUser(getCurrentUser().getUserName());
				resourseBizBean.setUpdatedUser(getCurrentUser().getUserName());
				Integer result = menuService.getIdByTitle(resourseBizBean.getTitle());
				if(null != result && result == -1){
					menuService.execute(resourseBizBean, BaseService.COMMAND_SAVE);
					result = menuService.getIdByTitle(resourseBizBean.getTitle());
					response.getWriter().print("{\"id\":\""+result+"\"}");
					log.info("增加菜单成功。。。");
				} else{
					response.getWriter().print("{\"id\":-2}");
					log.info("菜单已经存在。。。");
				}
				
			} else {
				response.getWriter().print("{\"msg\":false}");
				log.info("菜单实体为空,增加菜单失败！");
			}
		} catch (Exception e) {
			 e.printStackTrace();
		}
		return null;
	}
	
	public MenuService getMenuService() {
		return menuService;
	}
	@Resource
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	public ResourseBizBean getResourseBizBean() {
		return resourseBizBean;
	}
	public void setResourseBizBean(ResourseBizBean resourseBizBean) {
		this.resourseBizBean = resourseBizBean;
	}
}
